package com.os.ui;

import com.os.content.MemoryContent;
import com.os.content.SwapContent;
import com.os.pojo.vo.*;
import com.os.service.OsService;
import com.os.utils.ScreenUtils;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;
import javax.swing.border.TitledBorder;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.Objects;


public class MemoryProcessWindow {

    final int WIDTH = 1500;
    final int HEIGHT = 1000;

    static String[][] data;
    static String[] column = {"", "段号", "段大小", "实际占用大小", "是否加载"};
    static DefaultTableModel model;

    static String[][] data2;
    static String[] column2 = {"", "页号", "页内偏移", "块号", "是否加载", "内容", "LRU", "FIFO", "CLOCK"};

    static DefaultTableModel model2;

    // 表格
    static String[][] data3;
    static String[] column3 = {"物理地址", "进程名", "进程号", "进程段号", "页号", "块号", "存储内容"};
    static DefaultTableModel model3;

    // 请求分配表格
    static String[][] data4 = {};
    static String[] column4 = {"", "置换算法", "逻辑地址", "段号", "段命中", "页号", "页命中", "偏移量", "物理地址", "读取内容"};

    static DefaultTableModel model4;

    static OsService curOsService;

    static List<Integer> logicAddress;

    static Custom3DTextArea JTextListArea;

    static Custom3DTextArea JTextListArea2;

    static Integer pcbId;

//    static JLabel jLabelPrintRight;

    static JTextPane jLabelPrintRight;

    static JScrollPane jLabelPrintRightScrollPane;

    static JTextPane jFourLabel;

    static StringBuffer stringBuffer;

    static ArrayList<List<PageVo>> pageList = new ArrayList<List<PageVo>>();

    public void init(OsService osService, Integer memorySize, Integer pageSize) {
        curOsService = osService;
        JFrame jf = new JFrame("查看内存详情");
        Image icon = Toolkit.getDefaultToolkit().getImage("src/main/resources/任务进程.png");
        jf.setIconImage(icon);
        // 组装视图
        // 设置窗口相关属性
        // 设置窗口图标
        jf.setBounds((ScreenUtils.getScreenWidth() - WIDTH) / 2, (ScreenUtils.getScreenHeight() - HEIGHT) / 2, WIDTH, HEIGHT);
        jf.setResizable(false);

        JTabbedPane jTabbedPane = initTabbedPane(jf, memorySize, pageSize);
        jf.add(jTabbedPane);
        // 设置可视
        jf.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        jf.setVisible(true);
    }

    public static JTabbedPane initTabbedPane(JFrame jf, Integer memorySize, Integer pageSize) {
        JTabbedPane tabbedPane = new JTabbedPane();

        JPanel panel1 = initOnePanel();

        JPanel panel2 = initTwoPanel(memorySize, pageSize);

        JPanel panel3 = initThreePanel();

        JPanel panel4 = initFourPanel();

        tabbedPane.addTab("进程信息显示", panel1);
        tabbedPane.addTab("请求分配", panel3);
        tabbedPane.addTab("内存显示", panel2);
        tabbedPane.addTab("算法性能分析", panel4);

//        tabbedPane.addChangeListener(new ChangeListener() {
//            public void stateChanged(ChangeEvent e) {
//                stringBuffer = new StringBuffer();
//                jLabelPrintRight.setText(stringBuffer.toString());
//            }
//        });
        return tabbedPane;
    }

    /**
     * 初始化第一个页面
     *
     * @return
     */
    public static JPanel initOnePanel() {

        JPanel bottomPanel = initBottomBox();
        JPanel topPanel = initTopBox();
        JPanel panel = new JPanel();
        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS)); // 设置为垂直布局
        panel.add(topPanel);
        panel.add(bottomPanel);
        return panel;
    }

    /**
     * 初始化第一个页面顶部盒子
     *
     * @return
     */
    public static JPanel initTopBox() {
        // 设置窗口的内容
        JPanel topPanel = new JPanel();
        JPanel leftPanel = new JPanel();
        JPanel rightPanel = new JPanel();
        //  初始化顶部上侧左边元素
        Box leftTopBox = Box.createHorizontalBox();
//        JPanel jLabelPanel = new JPanel();
        Box leftBottomBox = Box.createHorizontalBox();
        JLabel jLabel = new JLabel("<html>请输入要显示的进程号:</html>");
        List<PcbInfoVo> pcbInfoList = curOsService.getPcbInfoList();
        String[] options = new String[pcbInfoList.size()];
        String[] names = new String[pcbInfoList.size()];
        for (int i = 0; i < pcbInfoList.size(); i++) {
            options[i] = String.valueOf(pcbInfoList.get(i).getId());
            names[i] = pcbInfoList.get(i).getName();
        }
        JComboBox<String> comboBox = new JComboBox<>(options);
        comboBox.setPreferredSize(new Dimension(300, comboBox.getPreferredSize().height)); // 设置按钮的宽度为 100 像素
        JButton jButton = new JButton("查询");
        jButton.setPreferredSize(new Dimension(100, jButton.getPreferredSize().height)); // 设置按钮的宽度为 100 像素


        // 添加顶部上侧左边元素
//        jLabelPanel.add(jLabel);
//        leftTopBox.add(jLabelPanel);
        leftTopBox.add(jLabel);
        leftTopBox.add(Box.createHorizontalStrut(20));
        leftBottomBox.add(comboBox);
        leftBottomBox.add(Box.createHorizontalStrut(20));
        leftBottomBox.add(jButton);
        leftPanel.add(leftTopBox);
//        leftPanel.add(Box.createHorizontalStrut(20));
        leftPanel.add(leftBottomBox);
        leftPanel.setLayout(new BoxLayout(leftPanel, BoxLayout.X_AXIS));
        leftPanel.setPreferredSize(new Dimension(600, leftPanel.getPreferredSize().height)); // 设置宽度为 500 像素

        // 初始化顶部上侧右边元素
        Box rightTopBox = Box.createHorizontalBox();
        TitledBorder titledBorder2 = new TitledBorder(new LineBorder(Color.GRAY, 1), "进程信息",
                TitledBorder.LEFT, TitledBorder.TOP, new Font("宋体", Font.PLAIN, 13), Color.BLACK);
        rightTopBox.setBorder(titledBorder2);
        Box rightTopInnerBox = Box.createHorizontalBox();
        rightTopInnerBox.setBorder(new EmptyBorder(10, 10, 10, 10));
        JLabel jLabel1 = new JLabel("Process Name:");
        JLabel jLabel2 = new JLabel("    ");
        JLabel jLabel3 = new JLabel("Process ID:");
        JLabel jLabel4 = new JLabel("  ");

        // 给查询按钮绑定函数
        jButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (comboBox.getSelectedItem() != null) {
                    // 获取下拉框的值
                    int intValue = Integer.parseInt(String.valueOf(comboBox.getSelectedItem())); // 将String类型转换为int类型
                    // 点击查询更新进程信息
                    jLabel2.setText(names[intValue]);
                    jLabel4.setText(options[intValue]);

                    // 点击查询更新段表信息
                    List<SegmentVo> segmentInfoByPcbId = curOsService.getSegmentInfoByPcbId(intValue);
                    data = new String[segmentInfoByPcbId.size()][4];
                    pageList.clear();
                    for (int j = 0; j < segmentInfoByPcbId.size(); j++) {
                        SegmentVo segmentVo = segmentInfoByPcbId.get(j);
                        pageList.add(segmentVo.getPageVoList());
                        data[j] = new String[]{j + 1 + "", segmentVo.getSegmentId() + "", segmentVo.getLen() + "", segmentVo.getSize() + "", segmentVo.isUsed() ? "Y" : "N"};
                    }
                    model.setDataVector(data, column);
                    model.fireTableDataChanged();
                }
            }
        });
        // 默认显示0号进程
        jButton.doClick();
        // 获取按钮的输入映射
        InputMap inputMap = jButton.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
        ActionMap actionMap = jButton.getActionMap();

        // 将回车键绑定到按钮的点击动作
        KeyStroke enterKey = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0, false);
        inputMap.put(enterKey, "clickButton");
        actionMap.put("clickButton", new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println(1);
                jButton.doClick();
            }
        });

        // 添加顶部上侧右边元素
        rightTopInnerBox.add(jLabel1);
        rightTopInnerBox.add(jLabel2);
        rightTopInnerBox.add(Box.createHorizontalStrut(100));
        rightTopInnerBox.add(jLabel3);
        rightTopInnerBox.add(jLabel4);
        rightTopBox.add(rightTopInnerBox);
        rightPanel.add(rightTopBox);

        topPanel.add(leftPanel);
        topPanel.add(Box.createHorizontalStrut(200));
        topPanel.add(rightPanel);
        // 设置下边框线
        topPanel.setBorder(new DashedBottomBorder(Color.BLACK));
        return topPanel;
    }


    /**
     * 初始化第一个页面底部盒子
     *
     * @return
     */
    public static JPanel initBottomBox() {
        // 设置窗口的内容
        JPanel bottomPanel = new JPanel();

        JPanel bottomLeftPanel = new JPanel();
        JPanel bottomRightPanel = new JPanel();

        // 左侧表格
//        String[][] data ={ {"1","q","2","3072,2048"},{"2","q","2","3072,2048"},{"3","q","2","3072,2048"}};
//        String[] column ={"","段号","段大小","是否加载","页表号"};
        model = new DefaultTableModel(data, column);
        ElementUITable jt = new ElementUITable(model);

        // 添加行选择监听器
        jt.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
            @Override
            public void valueChanged(ListSelectionEvent e) {
                if (!e.getValueIsAdjusting()) {
                    // 用户完成了行选择
                    int selectedRow = jt.getSelectedRow();
                    if (selectedRow >= 0) {
                        int intValue = Integer.parseInt(String.valueOf(jt.getValueAt(selectedRow, 1))); // 将String类型转换为int类型
                        if (intValue >= 0 && intValue < pageList.size()) {
                            List<PageVo> pageVos = pageList.get(intValue);
                            // 使用pageVos进行其他操作
                            data2 = new String[pageVos.size()][5];
                            // 在此处编写处理选中行的逻辑
                            for (int i = 0; i < pageVos.size(); i++) {
                                PageVo pageVo = pageVos.get(i);
//                        data2[i] = new String[]{i + 1 + "", pageVo.getPageId() + "", pageVo.getOffset() + "", pageVo.getFrameId() + "", pageVo.isUsed() ? "Y" : "N"};
                                data2[i] = new String[]{i + 1 + "", pageVo.getPageId() + "", pageVo.getLen() + "", pageVo.getFrameId() + "", pageVo.isUsed() ? "Y" : "N", pageVo.getChars(), pageVo.getLRUTime() + "", pageVo.getFIFOTime() + "", pageVo.isCLOCKBit() + ""};
                            }
                            model2.setDataVector(data2, column2);
                            model2.fireTableDataChanged();
                        } else {
                            // 处理索引越界的情况
                        }
                    }
                }
            }
        });

        JScrollPane scrollPane = new JScrollPane(jt);
        jt.setPreferredScrollableViewportSize(new Dimension(590, 700));
        bottomLeftPanel.add(scrollPane);

        // 右侧表格
//        String[][] data2 ={ {"1","q","2","3072,2048"},{"2","q","2","3072,2048"},{"3","q","2","3072,2048"}};
//        String[] column2 ={"","页号","页内偏移","块号","是否加载"};
        model2 = new DefaultTableModel(data2, column2);
        ElementUITable jt2 = new ElementUITable(model2);
        JScrollPane scrollPane2 = new JScrollPane(jt2);
        jt2.setPreferredScrollableViewportSize(new Dimension(790, 790));
        bottomRightPanel.add(scrollPane2);

        bottomPanel.add(bottomLeftPanel);
        bottomPanel.add(bottomRightPanel);
        return bottomPanel;
    }

    /**
     * 初始化第三个页面
     *
     * @return
     */
    public static JPanel initTwoPanel(Integer memorySize, Integer pageSize) {

        JPanel topPanel = initTopTwoBox(memorySize, pageSize);
        JPanel bottomPanel = initBottomTwoBox();
        JPanel panel = new JPanel();
        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS)); // 设置为垂直布局
        panel.add(topPanel);
        panel.add(bottomPanel);
        return panel;
    }

    /**
     * 初始化第三个页面顶部盒子
     *
     * @return
     */
    public static JPanel initTopTwoBox(Integer memorySize, Integer pageSize) {
        // 设置窗口的内容
        JPanel topPanel = new JPanel();
        topPanel.setPreferredSize(new Dimension(topPanel.getPreferredSize().width, 30)); // 设置按钮的宽度为 100 像素
        TitledBorder titledBorder = new TitledBorder(new LineBorder(Color.GRAY, 1), "内存信息",
                TitledBorder.LEFT, TitledBorder.TOP, new Font("宋体", Font.PLAIN, 13), Color.BLACK);
        topPanel.setBorder(titledBorder);
        //  顶部上侧左边
        JLabel jLabel1 = new JLabel("内存大小:");
        JLabel jLabel2 = new JLabel(memorySize + "");
        JLabel jLabel3 = new JLabel("总块数:");
        JLabel jLabel4 = new JLabel(memorySize / pageSize + "");
        JLabel jLabel5 = new JLabel("每块大小:");
        JLabel jLabel6 = new JLabel(pageSize + "");
        JButton jButton = new JButton("查询");
        // 给查询按钮绑定函数
        jButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                List<FrameVo> memoryFrameList = curOsService.getMemoryFrameList();
                data3 = new String[memoryFrameList.size()][7];
                for (int i = 0; i < memoryFrameList.size(); i++) {
                    FrameVo frameVo = memoryFrameList.get(i);
                    data3[i] = new String[]{i * MemoryContent.FRAME_SIZE + "", frameVo.getPcbName() + "", frameVo.getPcbId() + "", frameVo.getSegmentId() + "", frameVo.getPageId() + "", frameVo.getFrameId() + "", frameVo.getChars()};
                    model3.setDataVector(data3, column3);
                    model3.fireTableDataChanged();
                }
//                jLabel2.setText(String.valueOf(memoryFrameList.size()*4));
//                jLabel4.setText(String.valueOf(memoryFrameList.size()));
//                jLabel6.setText(String.valueOf(4));
//                System.out.println(memoryFrameList);
            }
        });

        JButton reset = new JButton("清空内存");
        // 给清空按钮绑定函数
        reset.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                curOsService.initMemory();
                jButton.doClick();
            }
        });


        // 获取按钮的输入映射
        InputMap inputMap = jButton.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
        ActionMap actionMap = jButton.getActionMap();

        // 将回车键绑定到按钮的点击动作
        KeyStroke enterKey = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0, false);
        inputMap.put(enterKey, "clickButton");
        actionMap.put("clickButton", new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                jButton.doClick();
            }
        });


        topPanel.add(jLabel1);
        topPanel.add(jLabel2);
        topPanel.add(Box.createHorizontalStrut(200));
        topPanel.add(jLabel3);
        topPanel.add(jLabel4);
        topPanel.add(Box.createHorizontalStrut(200));
        topPanel.add(jLabel5);
        topPanel.add(jLabel6);
        topPanel.add(Box.createHorizontalStrut(200));
        topPanel.add(jButton);
        topPanel.add(Box.createHorizontalStrut(200));
        topPanel.add(reset);
        return topPanel;
    }


    /**
     * 初始化第三个页面底部盒子
     *
     * @return
     */
    public static JPanel initBottomTwoBox() {
        // 设置窗口的内容
        JPanel bottomPanel = new JPanel();
        JPanel bottomBottomPanel = new JPanel();
        Box horizontalBox = Box.createHorizontalBox();
        // 上侧Label文字
        JLabel jLabel1 = new JLabel("已分配内存详情");
        // 字体放大，加粗
        Font font = new Font("宋体", Font.BOLD, 25);
        jLabel1.setFont(font);
        horizontalBox.add(jLabel1);
//        // 表格
//        String[][] data = {
//                {"1", "q", "1", "0", "3", "0"},
//                {"2", "q", "1", "0", "3", "0"},
//                {"3", "q", "1", "0", "3", "0"},
//                {"4", "q", "1", "0", "3", "0"},
//                {"5", "q", "1", "0", "3", "0"}
//        };
//        String[] column = {"", "进程名", "进程号", "进程段号", "页号", "块号"};
        model3 = new DefaultTableModel(data3, column3);
        ElementUITable jt = new ElementUITable(model3);
        JScrollPane scrollPane = new JScrollPane(jt);
        jt.setPreferredScrollableViewportSize(new Dimension(800, 770));
        Box horizontalBox2 = Box.createHorizontalBox();
        horizontalBox2.add(scrollPane);
        bottomBottomPanel.add(horizontalBox);
        bottomBottomPanel.add(horizontalBox2);
        bottomBottomPanel.setLayout(new BoxLayout(bottomBottomPanel, BoxLayout.Y_AXIS));
        bottomPanel.add(bottomBottomPanel);
        bottomPanel.setLayout(new BoxLayout(bottomPanel, BoxLayout.Y_AXIS)); // 设置为垂直布局
        return bottomPanel;
    }


    /**
     * 初始化第二个页面
     *
     * @return
     */
    public static JPanel initThreePanel() {
        JPanel leftPanel = initLeftThreeBox();
        JPanel rightPanel = initRightThreeBox();
        JPanel panel = new JPanel();
        panel.setBorder(new EmptyBorder(10, 10, 10, 10));
        panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS)); // 设置为水平布局
        panel.add(leftPanel);
        panel.add(Box.createHorizontalStrut(20));
        panel.add(rightPanel);
        return panel;
    }

    /**
     * 初始化第二个页面左侧信息栏
     *
     * @return
     */
    private static JPanel initLeftThreeBox() {
        // 设置窗口的内容
        JPanel leftPanel = new JPanel();
        JPanel containPanel = new JPanel();
        TitledBorder titledBorder = new TitledBorder(new LineBorder(Color.GRAY, 1), "",
                TitledBorder.LEFT, TitledBorder.TOP, new Font("宋体", Font.PLAIN, 13), Color.BLACK);
        leftPanel.setBorder(titledBorder);
        String[] texts = {"<html>请输入要显示的进程号:</html>", "<html>请选择段号:</html>", "<html>请选择页号:</html>", "<html>请选择页内偏移:</html>", "<html>请选择置换算法:</html>", "<html>请输入分配序列:</html>"};

        // 初始化显示置换算法下拉框
        Box box4 = Box.createVerticalBox();
        JLabel jLabel4 = new JLabel(texts[4]);
        String[] options4 = {SwapContent.FIFO, SwapContent.LRU, SwapContent.CLOCK};
        // 获取置换算法值
        JComboBox<String> comboBox4 = new JComboBox<>(options4);
        JButton jButton = new JButton("查询");
        jButton.setPreferredSize(new Dimension(320, 30)); // 设置按钮的宽度为 300 像素

        Box boxTop = Box.createHorizontalBox();
        boxTop.add(jLabel4);
        boxTop.add(Box.createHorizontalStrut(200));

        JPanel jPanel = new JPanel();
        jPanel.add(jButton);
        box4.add(jPanel);
        box4.add(Box.createVerticalStrut(5));
        box4.add(boxTop);
        box4.add(Box.createVerticalStrut(5));
        box4.add(comboBox4);

        // 初始化显示页内偏移下拉框
        Box box3 = Box.createVerticalBox();
        JLabel jLabel3 = new JLabel(texts[3]);
        Integer[] options3 = new Integer[0];
        // 获取页内偏移值
        JComboBox<Integer> comboBox3 = new JComboBox<>(options3);

        Box boxTop1 = Box.createHorizontalBox();
        boxTop1.add(jLabel3);
        boxTop1.add(Box.createHorizontalStrut(200));
        box3.add(boxTop1);
        box3.add(Box.createVerticalStrut(5));
        box3.add(comboBox3);
        box3.add(Box.createVerticalStrut(5));


        // 初始化显示页号下拉框
        Box box2 = Box.createVerticalBox();
        JLabel jLabel2 = new JLabel(texts[2]);
        Integer[] options2 = new Integer[0];
        // 获取段号值
        JComboBox<Integer> comboBox2 = new JComboBox<>(options2);


        Box boxTop2 = Box.createHorizontalBox();
        boxTop2.add(jLabel2);
        boxTop2.add(Box.createHorizontalStrut(230));
        box2.add(boxTop2);
        box2.add(Box.createVerticalStrut(5));
        box2.add(comboBox2);
        box2.add(Box.createVerticalStrut(5));

        // 初始化显示段号下拉框
        Box box1 = Box.createVerticalBox();
        JLabel jLabel1 = new JLabel(texts[1]);
        Integer[] options1 = new Integer[0];
        // 获取段号值
        JComboBox<Integer> comboBox1 = new JComboBox<>(options1);


        Box boxTop3 = Box.createHorizontalBox();
        boxTop3.add(jLabel1);
        boxTop3.add(Box.createHorizontalStrut(230));
        box1.add(boxTop3);
        box1.add(Box.createVerticalStrut(5));
        box1.add(comboBox1);
        box1.add(Box.createVerticalStrut(5));

        // 初始化显示进程号下拉框
        Box box = Box.createVerticalBox();
        JLabel jLabel = new JLabel(texts[0]);
        List<PcbInfoVo> pcbInfoList = curOsService.getPcbInfoList();
        Integer[] options = new Integer[pcbInfoList.size()];
        for (int i = 0; i < pcbInfoList.size(); i++) {
            options[i] = pcbInfoList.get(i).getId();
        }
        JComboBox<Integer> comboBox = new JComboBox<>(options);

        Box boxTop4 = Box.createHorizontalBox();
        boxTop4.add(jLabel);
        boxTop4.add(Box.createHorizontalStrut(160));
        box.add(boxTop4);
        box.add(Box.createVerticalStrut(5));
        box.add(comboBox);
        box.add(Box.createVerticalStrut(5));

        // 设置监听函数
        comboBox.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                Integer selectedItem = (Integer) comboBox.getSelectedItem();
                if (selectedItem != null) {
                    // 在这里可以对所选值执行想要的操作
                    PcbInfoVo pcbInfoVo = pcbInfoList.get(selectedItem);
                    List<SegmentVo> segmentInfoByPcbId = curOsService.getSegmentInfoByPcbId(pcbInfoVo.getId());
                    Integer[] nums = new Integer[segmentInfoByPcbId.size() + 1];
                    nums[0] = -1;
                    for (int i = 1; i <= segmentInfoByPcbId.size(); i++) {
                        nums[i] = segmentInfoByPcbId.get(i - 1).getSegmentId();
                    }
                    comboBox1.setModel(new DefaultComboBoxModel<>(nums));
                }
            }
        });
        comboBox1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                Integer selectedItem = (Integer) comboBox.getSelectedItem();
                Integer selectedItem1 = (Integer) comboBox1.getSelectedItem();
                if (selectedItem1 == -1) {
                    comboBox2.setSelectedItem(-1);
                }
                if (selectedItem1 != null && selectedItem != null && selectedItem1 != -1) {
                    // 在这里可以对所选值执行想要的操作
                    int id = curOsService.getPcbInfoList().get(selectedItem).getId();
                    List<SegmentVo> segmentInfoByPcbId = curOsService.getSegmentInfoByPcbId(id);
                    SegmentVo segmentVo = segmentInfoByPcbId.get(selectedItem1);
                    List<PageVo> pageVoList = segmentVo.getPageVoList();
                    Integer[] nums = new Integer[pageVoList.size() + 1];
                    nums[0] = -1;
                    for (int i = 1; i <= pageVoList.size(); i++) {
                        nums[i] = pageVoList.get(i - 1).getPageId();
                    }
                    comboBox2.setModel(new DefaultComboBoxModel<>(nums));
                }
            }
        });

        comboBox2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                Integer selectedItem = (Integer) comboBox.getSelectedItem();
                Integer selectedItem1 = (Integer) comboBox1.getSelectedItem() == -1 ? null : (Integer) comboBox1.getSelectedItem();
                Integer selectedItem2 = (Integer) comboBox2.getSelectedItem();
                if (selectedItem2 == -1) {
                    comboBox3.setSelectedItem(-1);
                }
                if (selectedItem2 != null && selectedItem1 != null && selectedItem != null && selectedItem2 != -1) {
                    // 在这里可以对所选值执行想要的操作
                    int id = curOsService.getPcbInfoList().get(selectedItem).getId();
                    List<SegmentVo> segmentInfoByPcbId = curOsService.getSegmentInfoByPcbId(id);
                    SegmentVo segmentVo = segmentInfoByPcbId.get(selectedItem1);
                    List<PageVo> pageVoList = segmentVo.getPageVoList();
                    PageVo pageVo = pageVoList.get(selectedItem2);

                    Integer[] nums = new Integer[pageVo.getLen() + 1];
                    nums[0] = -1;
                    for (int i = 0; i < pageVo.getLen(); i++) {
                        nums[i + 1] = i;
                    }
                    comboBox3.setModel(new DefaultComboBoxModel<>(nums));
                }
            }
        });

        // 添加事件监听器
        jButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // 打印四个下拉框的信息
                Integer selectedItem = (Integer) comboBox.getSelectedItem();
                Integer selectedItem1 = (Integer) comboBox1.getSelectedItem() == -1 ? null : (Integer) comboBox1.getSelectedItem();
                Integer selectedItem2 = null;
                Integer selectedItem3 = null;
                if (selectedItem1 != null && selectedItem1 != -1 && (Integer) comboBox2.getSelectedItem() != null) {
                    selectedItem2 = (Integer) comboBox2.getSelectedItem() == -1 ? null : (Integer) comboBox2.getSelectedItem();
                }
                if (selectedItem2 != null && selectedItem2 != -1 && (Integer) comboBox3.getSelectedItem() != null) {
                    selectedItem3 = (Integer) comboBox3.getSelectedItem() == -1 ? null : (Integer) comboBox3.getSelectedItem();
                }
                pcbId = selectedItem;
                logicAddress = curOsService.getLogicAddress(selectedItem, selectedItem1, selectedItem2, selectedItem3);
//                String valueByAddressList = curOsService.getValueByAddressList(selectedItem, logicAddress);
                JTextListArea.setText(logicAddress.toString().replace("[", "").replace("]", ""));
                JTextListArea2.setText(logicAddress.toString().replace("[", "").replace("]", ""));
//                System.out.println(valueByAddressList);
                System.out.println(logicAddress);
//                curOsService.changeSwapAlgorithm(selectedItem4);
//                System.out.println(selectedItem);
//                System.out.println(selectedItem1);
//                System.out.println(selectedItem2);
//                System.out.println(selectedItem3);
//                System.out.println(selectedItem4);
            }
        });

        // 获取按钮的输入映射
        InputMap inputMap = jButton.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
        ActionMap actionMap = jButton.getActionMap();

        // 将回车键绑定到按钮的点击动作
        KeyStroke enterKey = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0, false);
        inputMap.put(enterKey, "clickButton");
        actionMap.put("clickButton", new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println(1);
                jButton.doClick();
            }
        });

        // 设置comboBox初始值
        comboBox.setSelectedItem(0);
        comboBox1.setSelectedItem(-1);
        comboBox2.setSelectedItem(-1);
        comboBox3.setSelectedItem(-1);

        Box box5 = Box.createVerticalBox();
        JPanel labelJPanel = new JPanel();
        JPanel topPanel = new JPanel();
        JLabel jLabelInput = new JLabel(texts[5]);
        JTextListArea = new Custom3DTextArea();
        JTextListArea.setColumns(20);
        JTextListArea.setRows(20);
        JTextListArea.setLineWrap(true); // 启用自动换行
        JTextListArea.setWrapStyleWord(true); // 启用以单词为单位的换行
        JScrollPane areaScrollPane = new JScrollPane(JTextListArea);
        labelJPanel.add(jLabelInput);
        labelJPanel.add(Box.createHorizontalStrut(200));

        topPanel.add(labelJPanel);
        topPanel.add(Box.createVerticalStrut(5));
        topPanel.add(areaScrollPane);
        topPanel.setLayout(new BoxLayout(topPanel, BoxLayout.Y_AXIS));
        box5.add(topPanel);

        // 分配按钮
        JPanel visitJpanel = new JPanel();
        JButton visitjButton = new JButton("分配");
        visitjButton.setPreferredSize(new Dimension(320, 30)); // 设置按钮的宽度为 300 像素
        visitJpanel.add(visitjButton);
        // 添加事件监听器
        visitjButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // 打印四个下拉框的信息
                Integer selectedItem = (Integer) comboBox.getSelectedItem();
                String text = JTextListArea.getText().replace(" ", "");
                System.out.println(selectedItem);
                System.out.println(text);
                String[] split = text.split(",");
                logicAddress = new ArrayList<>();
                for (String s : split) {
                    if (Objects.equals(s, "")) {
                        //弹出警告框
                        JOptionPane.showMessageDialog(leftPanel, "未输入分配序列！", "提示", JOptionPane.WARNING_MESSAGE);
                        return;
                    }
                    try {
                        logicAddress.add(Integer.valueOf(s));
                    } catch (NumberFormatException e1) {
                        JOptionPane.showMessageDialog(topPanel, "分配序列输入有误！", "提示", JOptionPane.WARNING_MESSAGE);
                        return;
                    }
                }
                String selectedItem1 = (String) comboBox4.getSelectedItem();
                System.out.println(selectedItem1);
                curOsService.changeSwapAlgorithm(selectedItem1);
                String valueByAddressList = curOsService.getValueByAddressList(selectedItem, logicAddress);
                List<ActionVo> actionList = curOsService.getActionList();
                data4 = new String[actionList.size()][10];
                for (int i = 0; i < actionList.size(); i++) {
                    ActionVo actionVo = actionList.get(i);
                    data4[i] = new String[]{String.valueOf(actionVo.getId()), actionVo.getSwapAlgorithm(), String.valueOf(actionVo.getLogicAddress()), String.valueOf(actionVo.getSegmentId()), String.valueOf(actionVo.isHasSegmentFocus()), String.valueOf(actionVo.getPageId()), String.valueOf(actionVo.isHasPageFocus()), String.valueOf(actionVo.getOffset()), String.valueOf(actionVo.getPhysicalAddress()), actionVo.getMsg()};
                }
                model4.setDataVector(data4, column4);
                model4.fireTableDataChanged();
                System.out.println(actionList);
//                JTextListArea.setText(logicAddress.toString().replace("[","").replace("]",""));
                System.out.println(valueByAddressList);
                stringBuffer = new StringBuffer();
                stringBuffer.append(valueByAddressList);
                // 获取需要显示的多行文本
                String multiLineText = stringBuffer.toString(); // 这里替换成你需要显示的多行文本内容
                JScrollBar verticalScrollBar = jLabelPrintRightScrollPane.getVerticalScrollBar();
                int currentValue = verticalScrollBar.getValue(); // 获取当前滚动条位置
                jLabelPrintRight.setText(multiLineText);
                SwingUtilities.invokeLater(() -> {
                    verticalScrollBar.setValue(currentValue); // 恢复滚动条到之前的位置
                });
//                jLabelPrintRight.setText("<html><p style=\"width: 800px; word-wrap: break-word;\">" + multiLineText + "</p></html>");

//                System.out.println(logicAddress);
//                curOsService.changeSwapAlgorithm(selectedItem4);
            }
        });


        containPanel.add(Box.createVerticalStrut(10));
        containPanel.add(box);
        containPanel.add(Box.createVerticalStrut(10));
        containPanel.add(box1);
        containPanel.add(Box.createVerticalStrut(10));
        containPanel.add(box2);
        containPanel.add(Box.createVerticalStrut(10));
        containPanel.add(box3);
        containPanel.add(Box.createVerticalStrut(10));
        containPanel.add(box4);
        containPanel.add(Box.createVerticalStrut(10));
        containPanel.add(box5);
        containPanel.add(Box.createVerticalStrut(10));
        containPanel.add(visitJpanel);
        containPanel.setLayout(new BoxLayout(containPanel, BoxLayout.Y_AXIS));
        leftPanel.add(containPanel);
        return leftPanel;
    }

    /**
     * 初始化第二个页面右侧打印栏
     *
     * @return
     */
    private static JPanel initRightThreeBox() {
        // 设置窗口的内容
        JPanel rightPanel = new JPanel();
        rightPanel.setPreferredSize(new Dimension(1000, 800)); // 设置宽度为 1000 像素
        TitledBorder titledBorder1 = new TitledBorder(new LineBorder(Color.GRAY, 1), "",
                TitledBorder.LEFT, TitledBorder.TOP, new Font("宋体", Font.PLAIN, 13), Color.BLACK);
        rightPanel.setBorder(titledBorder1);

        JPanel panel = new JPanel();
        //  打印框
//        Box boxBottom = Box.createVerticalBox();
        stringBuffer = new StringBuffer();
//        jLabelPrintRight = new JLabel(stringBuffer.toString());
        jLabelPrintRight = new JTextPane();
        jLabelPrintRight.setText(stringBuffer.toString());

//        jLabelPrintRight.setPreferredSize(new Dimension(1000, 900));
        jLabelPrintRightScrollPane = new JScrollPane(jLabelPrintRight);
        jLabelPrintRightScrollPane.setPreferredSize(new Dimension(1000, 400));
//        jLabelPrintRightScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
//        boxBottom.add(jLabelPrintRightScrollPane);
//
//        rightPanel.add(boxBottom);

        model4 = new DefaultTableModel(data4, column4);
        ElementUITable bottomTable = new ElementUITable(model4);
        JScrollPane scrollPane = new JScrollPane(bottomTable);
        scrollPane.setPreferredSize(new Dimension(1000, 470));

//        bottomTable.setVisible(false);
//        jLabelPrintRightScrollPane.setVisible(false);
        // 将 JScrollPane 添加到右侧容器的中间位置
        panel.add(jLabelPrintRightScrollPane);
        panel.add(Box.createVerticalStrut(20));
        panel.add(scrollPane);
        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
        rightPanel.add(panel, BorderLayout.CENTER);

        return rightPanel;
    }

    /**
     * 初始化第四个页面
     *
     * @return
     */
    public static JPanel initFourPanel() {
        JPanel topPanel = initTopFourBox();
        JPanel bottomPanel = initBottomFourBox();
        JPanel panel = new JPanel();
        panel.setBorder(new EmptyBorder(10, 10, 10, 10));
        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS)); // 设置为水平布局
        panel.add(topPanel);
        panel.add(Box.createVerticalStrut(20));
        panel.add(bottomPanel);
        return panel;
    }

    /**
     * 初始化第四个页面顶部盒子
     *
     * @return
     */
    public static JPanel initTopFourBox() {
        // 设置窗口的内容
        JPanel topPanel = new JPanel();
        topPanel.setPreferredSize(new Dimension(topPanel.getPreferredSize().width, 200)); // 设置高度为 200像素
        TitledBorder titledBorder = new TitledBorder(new LineBorder(Color.GRAY, 1), "内存信息",
                TitledBorder.LEFT, TitledBorder.TOP, new Font("宋体", Font.PLAIN, 13), Color.BLACK);
        topPanel.setBorder(titledBorder);
        //  顶部上侧左边
        JLabel jLabel1 = new JLabel("访问序列(空格或逗号分割):");
        JTextListArea2 = new Custom3DTextArea();
        JTextListArea2.setColumns(100);
        JTextListArea2.setRows(10);
        JTextListArea2.setLineWrap(true); // 启用自动换行
        JTextListArea2.setWrapStyleWord(true); // 启用以单词为单位的换行
        JScrollPane areaScrollPane = new JScrollPane(JTextListArea2);
        JButton jButton = new JButton("访问");
        JButton jButton2 = new JButton("打乱访问序列");

        jButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String text = JTextListArea2.getText().replace(" ", "");
                String[] split = text.split(",");
                logicAddress = new ArrayList<>();
                for (String s : split) {
                    if (Objects.equals(s, "")) {
                        //弹出警告框
                        JOptionPane.showMessageDialog(topPanel, "未输入访问序列！", "提示", JOptionPane.WARNING_MESSAGE);
                        return;
                    }
                    try {
                        logicAddress.add(Integer.valueOf(s));
                    } catch (NumberFormatException e1) {
                        JOptionPane.showMessageDialog(topPanel, "访问序列输入有误！", "提示", JOptionPane.WARNING_MESSAGE);
                        return;
                    }
                }
                String s = curOsService.countAlgorithm(pcbId, logicAddress);
                jFourLabel.setText(s);
                System.out.println(s);
            }
        });
        jButton2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String curText = JTextListArea2.getText().replace(" ", "");
                String[] curSplit = curText.split(",");
                logicAddress = new ArrayList<>();
                for (String s : curSplit) {
                    if (Objects.equals(s, "")) {
                        //弹出警告框
                        JOptionPane.showMessageDialog(topPanel, "未输入访问序列！", "提示", JOptionPane.WARNING_MESSAGE);
                        return;
                    }
                    try {
                        logicAddress.add(Integer.valueOf(s));
                    } catch (NumberFormatException e1) {
                        JOptionPane.showMessageDialog(topPanel, "访问序列输入有误！", "提示", JOptionPane.WARNING_MESSAGE);
                        return;
                    }
                }
                curOsService.shuttleList(logicAddress);
                JTextListArea2.setText(logicAddress.toString().replace("[", "").replace("]", ""));
            }
        });


        // 获取按钮的输入映射
        InputMap inputMap = jButton.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
        ActionMap actionMap = jButton.getActionMap();

        // 将回车键绑定到按钮的点击动作
        KeyStroke enterKey = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0, false);
        inputMap.put(enterKey, "clickButton");
        actionMap.put("clickButton", new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                jButton.doClick();
            }
        });

        topPanel.add(jLabel1);
        topPanel.add(areaScrollPane);
        topPanel.add(Box.createHorizontalStrut(10));
        topPanel.add(jButton);
        topPanel.add(jButton2);
        return topPanel;
    }


    /**
     * 初始化第四个页面底部盒子
     *
     * @return
     */
    public static JPanel initBottomFourBox() {
        // 设置窗口的内容
        JPanel bottomPanel = new JPanel();
        bottomPanel.setPreferredSize(new Dimension(bottomPanel.getWidth(), 600));
        TitledBorder titledBorder = new TitledBorder(new LineBorder(Color.GRAY, 1), "",
                TitledBorder.LEFT, TitledBorder.TOP, new Font("宋体", Font.PLAIN, 13), Color.BLACK);
        bottomPanel.setBorder(titledBorder);
        //  打印框
        Box box = Box.createVerticalBox();
        jFourLabel = new JTextPane();
        jFourLabel.setPreferredSize(new Dimension(1400, 620));
        box.add(jFourLabel);
        bottomPanel.add(box);
        return bottomPanel;
    }


    /**
     * 设置全局字体
     *
     * @param f
     */
    public static void setUIFont(javax.swing.plaf.FontUIResource f) {
        // 获取默认的 UI 风格
        UIDefaults defaults = UIManager.getDefaults();

        // 获取所有键的枚举
        Enumeration<Object> keys = defaults.keys();
        while (keys.hasMoreElements()) {
            Object key = keys.nextElement();
            Object value = defaults.get(key);
            if (value instanceof javax.swing.plaf.FontUIResource) {
                defaults.put(key, f);
            }
        }
    }

//    public static void main(String[] args) {
//        // 设置全局字体大小为 14
//        setUIFont(new javax.swing.plaf.FontUIResource("宋体",Font.PLAIN,15));
//
//        // 创建并显示 GUI 界面
//        SwingUtilities.invokeLater(() -> {
//            MemoryProcessWindow memoryProcessWindow = new MemoryProcessWindow();
//            memoryProcessWindow.init("16","2");
//        });
//    }

}
